package com.eric.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.eric.entity.User;
import com.eric.result.Code;
import com.eric.result.Result;
import com.eric.service.GetUserInfoService;
import com.eric.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Spliterator;

@Controller
@ResponseBody
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private GetUserInfoService getUserInfoService;

    @PostMapping("/saveOne")
    public SaResult saveOne(@RequestBody User user) {
//        hashpw方法，用于加密元素数据，第一个单数是原始密码，BCrypt.gensalt方法是生成盐，返回的是
//        一个加密后的字符串
//        String hashpw = BCrypt.hashpw("12345", BCrypt.gensalt());
//        System.out.println(hashpw);
//       checkpw（）方法，用于判断传进来的字符串与数据库中的加密过的字符串是否匹配，返回是一个boolean值
//        第一个参数是传进来的字符串，第二个参数是加密后的字符串
//        boolean checkpw = BCrypt.checkpw("12345", "$2a$10$yCJt3iPNt.asjYY1P/qb6OLio3vt980sxFfXJxPw1q7N0uTI7WSY2");
//        System.out.println(checkpw);
        System.out.println(user);

        if (getUserInfoService.getONeByNameService(user.getLoginname()) == null) {
            String password = user.getPassword();
            String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
            user.setPassword(hashpw);
            userInfoService.saveOneUserService(user);
            return SaResult.ok("注册成功！");
        }

             return SaResult.error("用户已存在，请换一个用户名注册");
    }


    @PutMapping("/modifyOneUser")
    public SaResult modifyOneUser( @RequestBody Map<String,Object> userMap){
        Result result = userInfoService.modifyOneUserService(userMap);
        Integer code = result.getCode();
        if (code.equals(Code.PUT_ERR)){
            return SaResult.error(result.getMsg());
        }
        return SaResult.ok(result.getMsg());
    }

    @PutMapping("/modifyOneUserRole")
    public SaResult modifyOneRole(Integer userId,Integer role_id){
        Result result = userInfoService.modifyOneRoleService(userId, role_id);
        System.out.println(result);
        return SaResult.ok(result.getMsg());
    }

//    权鉴接口测试
    @RequestMapping("/getRolesPermissions")
    public SaResult getRolesPermissions(){
        System.out.println(StpUtil.getPermissionList());
        System.out.println(StpUtil.hasPermission("user.delete"));
        StpUtil.checkPermission("user.add");
        StpUtil.checkPermissionAnd("user.delete","user.modify");
        StpUtil.checkPermissionOr("user.delete","user.search");

        System.out.println(StpUtil.getRoleList());
        System.out.println(StpUtil.hasRole("admin"));
        StpUtil.checkRole("normal");
        StpUtil.checkRoleAnd("normal");
        StpUtil.checkRoleOr("admin");
        return SaResult.ok(StpUtil.getPermissionList().toString());
    }

//    角色权限双重“or校验”
    @RequestMapping("userAdd")
//    代表具有三个角色其一即可
//    @SaCheckPermission(value="user.add",orRole ={"admin","manager","normal"} )
    //    代表三个角色必须同时具有才可以
    @SaCheckPermission(value="user.add",orRole ={"admin,manager,normal"} )
    public SaResult userAdd(){

        return SaResult.data("用户信息");
    }


}
